import { Tools } from "../../common/Tools";
import { EventManager } from "../../manager/EventManager";
import LvItem from "../LvItem";
import LvItem_15 from "./LvItem_15";
import { EVENT_NAME_ENUM } from "../../base/EnumIndex";


const { ccclass, property } = cc._decorator;

@ccclass
export default class moneyDui_36 extends cc.Component {
    /** 当前选中的钱 */
    @property(cc.Node)
    selectMoeny: cc.Node = null;


    /** 当前 操作的钱 */
    touchMoeny: cc.Node = null;
    onLoad() {
        this.initAllTouch();
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
    }
    /** 初始化所有节点的触摸 */
    initAllTouch() {
        for (let i = 0; i < this.node.children.length; i++) {
            const node = this.node.children[i];
            node["initPos"] = node.position.clone();

            node.on(cc.Node.EventType.TOUCH_START, this.touchStartBack, this);
            node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveBack, this);
            node.on(cc.Node.EventType.TOUCH_END, this.touchEndBack, this);
            node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndBack, this);
        }

    };

    touchStartPos: cc.Vec3 = null;
    touchStartBack(touchs: cc.Event.EventTouch) {
        if (this.node.children.length <= 0) { return; }
        this.touchStartPos = cc.v3(touchs.getLocation());

        this.touchMoeny = this.node.children[this.node.children.length - 1];

        this.selectMoeny.getComponent(LvItem_15).touchStartPos = this.touchStartPos;
        this.selectMoeny.position = cc.v3(Tools.getToNodePosForWorld(touchs.getLocation(), this.selectMoeny.parent));

        Tools.setNodeParent(this.touchMoeny, this.selectMoeny);
        this.touchMoeny.position = cc.v3(0, 0, 0);
    };

    touchMoveBack(touchs: cc.Event.EventTouch) {
        if (!this.touchStartPos || !this.selectMoeny) { return; }

        this.selectMoeny.getComponent(LvItem_15).touchMoveBack(touchs);
    };
    touchEndBack(touchs: cc.Event.EventTouch) {
        if (!this.touchStartPos || !this.selectMoeny) { return; }

        if (this.selectMoeny.children.length > 0) {
            let moeny = this.touchMoeny;
            Tools.setNodeParent(moeny, this.node);
            moeny.setSiblingIndex(this.node.children.length);
            moeny.position = moeny["initPos"];
        }
        this.selectMoeny.getComponent(LvItem_15).touchMoveEndBack(touchs);
        this.selectMoeny.getComponent(LvItem_15).returnInitPos();

        this.touchStartPos = null;
        this.touchMoeny = null;
    };
    finishTween(finishStr: string, lvItem: LvItem) {
        if (typeof finishStr != "string") {
            return;
        }
        if (finishStr.includes("placeMoney")) {
            this.node.children[this.node.children.length - 1].destroy();
        }
    };

    protected onDestroy(): void {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
    }

}
